package com.railwayservice.common.web;

import com.railwayservice.application.config.AppConfig;
import com.railwayservice.application.util.RSAEncryptUtil;
import com.railwayservice.application.util.RandomString;
import com.railwayservice.application.vo.LoginMessage;
import com.railwayservice.application.vo.ResultMessage;
import com.railwayservice.application.vo.RsaKeyData;
import com.railwayservice.common.entity.Dictionary;
import com.railwayservice.common.entity.ImageInfo;
import com.railwayservice.common.entity.Location;
import com.railwayservice.common.service.CommonService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 业务控制器。
 *
 * @author Ewing
 */
@Controller
@RequestMapping(value = "/common", method = {RequestMethod.GET,RequestMethod.POST})
@Api(value = "业务控制器", description = "业务控制器")
public class CommonController {
    private final Logger logger = LoggerFactory.getLogger(CommonController.class);

    private CommonService commonService;

    @Autowired
    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }

    /**
     * 登陆时获取公钥。
     */
    @ResponseBody
    @RequestMapping(value = "/rsaKey", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMessage getRsaKey() {
        logger.info("登陆前获取RsaKey");

        RsaKeyData rsaKeyData = RSAEncryptUtil.getPublicKeyData();
        return ResultMessage.newSuccess().setData(rsaKeyData);

    }

    /**
     * 登陆时获取随机盐。
     */
    @ResponseBody
    @RequestMapping(value = "/sessionSalt", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMessage getSessionSalt(HttpServletRequest request) {
        logger.info("登陆时获取随机盐");

        String salt = RandomString.randomString(10);
        request.getSession().setAttribute("sessionSalt", salt);
        return ResultMessage.newSuccess().setData(new LoginMessage(salt));

    }

    /**
     * Session退出登陆。
     *
     * @return ResultMessage
     * @author xuy
     * @date 2017.2.14
     */
    @ResponseBody
    @RequestMapping(value = "/sessionLogout", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMessage sessionLogout(HttpServletRequest request) {
        logger.info("Session退出登陆：" + request.getRemoteAddr());

        request.getSession().removeAttribute(AppConfig.ADMIN_SESSION_KEY);
        return ResultMessage.newSuccess();

    }

    /**
     * 根据字典类型获取字典值。
     */
    @ResponseBody
    @RequestMapping(value = "/dictionary/{type}", method = {RequestMethod.GET,RequestMethod.POST})
    public List<Dictionary> dictionary(@PathVariable("type") String type) {
        logger.info("根据字典类型获取字典值：");

        return commonService.findDictionaryByType(type);

    }

    /**
     * 根据上级位置获取下级位置列表。
     */
    @ResponseBody
    @RequestMapping(value = "/location/{parent}", method = {RequestMethod.GET,RequestMethod.POST})
    public List<Location> location(@PathVariable("parent") String parent) {
        logger.info("根据上级位置获取下级位置列表：");

        return commonService.findLocationByParentId(parent);

    }

    /**
     * 添加图片的方法。
     */
    @ResponseBody
    @RequestMapping(value = "/addImage", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMessage addImage(MultipartFile imageFile) {
        logger.info("添加图片的方法：");
        try {
            ImageInfo img = commonService.addImage(imageFile.getInputStream());
            return ResultMessage.newSuccess("添加图片成功！").setData(img);
        } catch (Exception e) {
            logger.error("添加图片异常：", e);
            return ResultMessage.newFailure("添加图片失败！");
        }
    }

    /**
     * 根据图片ID查询图片。
     */
    @ResponseBody
    @RequestMapping(value = "/getImageUrl", method = {RequestMethod.GET,RequestMethod.POST})
    public ResultMessage getImageUrl(String imageId) {
        logger.info("根据图片ID查询图片：");

        String url = commonService.getImageUrl(imageId);
        return ResultMessage.newSuccess().setData(url);

    }

}
